package handin_6_Vending_Machine;

import java.util.Scanner;

public class Vending_Machine {
	
	public static void main(String args[])
	{
		//The inventory and default credits are set by an operator:
		Inventory mars = new Inventory("Marsbar", 10,11);
		Inventory snickers = new Inventory("Snickers", 8,9.5);
		Inventory juice = new Inventory("Juicebox", 1,8);
			
		Credit account = new Credit(0);
		
		//The system is turned and the interface is initialized:
		boolean powerOn = true;
		Scanner in = new Scanner(System.in);
		
		//As long as the machine is powered on it doesn't terminate.
		//This is main process of the vending machine:
		
		while(powerOn)
		{
			//Prints options to the user
			System.out.println("Please input coins(by value or name) or choose an item from the list:");
			System.out.println(mars.toString());
			System.out.println(snickers.toString());
			System.out.println(juice.toString());
			System.out.println("Your credit is currently: "+ account.toString());
		
			//Checks if the input is a double
			if(in.hasNextDouble())
			{	
				//Attempts to add the input to credits. This will only succede
				//if the input is a valid coin. See j-doc for the method.
				account.add(in.nextDouble());

			}
			
			//If the input was not a double, but there was in input:
			else if(in.hasNext())
			{
				String choice = in.next();
				
				//Checks if the input is contained in the "commands" avaliable in the machines inventory.
				if(Inventory.commands.contains(choice))
				{
					//If it is, each inventory obejct is checks and it is checked if suficcient credit is avaliable.
					if(choice.equals(mars.name) && account.check()>=mars.price)
					{
						//If the item is released(ie. not sold out), the credits are substracted and the remainder is cashed out.
						if(mars.release())
							{
							account.remove(mars.price);
							}
						account.payout();
					}
					else if(choice.equals(snickers.name)&& account.check()>=snickers.price)
					{
						
						if(snickers.release())
							{
							account.remove(snickers.price);
							}
						account.payout();
					}
					else if(choice.equals(juice.name)&& account.check()>=juice.price)
					{
						if(juice.release())
							{
							account.remove(juice.price);
							}						
						account.payout();
					}
					else
					{
					System.out.println("Insufficient funds.");
					account.payout();
					}
				}
				
				//If the user request an item that was not it will be interperted as s coin name.
				//An attempt is made to add the coin value to credits:
				else{
				account.add(choice);
				}
			}
			//The machine is still powered so it will repeat the cycle ontil somebody pulls the plug.
		}

		
		
		
	}

}
